package com.logic.modular.game.statistical.service.impl;

import com.comment.dubbo.service.PlayerStatisticalPool2Service;
import com.comment.pojo.domain.logic.domain.PlayerHero;
import com.logic.modular.game.battle.info.base.PvpPlayerInfo;
import com.logic.modular.game.player.hero.mapper.PlayerHeroMapper;
import com.logic.modular.game.player.skill.mapper.PlayerSkillMapper;
import com.logic.modular.gameCore.cultivate.exp.CoreExperienceServiceImpl;
import com.logic.modular.gameCore.cultivate.hero.service.CoreHeroServiceImpl;
import com.logic.modular.gameCore.cultivate.hero.service.CoreHeroStarServiceImpl;
import com.logic.modular.gameCore.cultivate.skill.controller.CoreSkillStarServiceImpl;
import lombok.extern.slf4j.Slf4j;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.logic.modular.game.statistical.mapper.PlayerStatisticalPool2Mapper;
import com.logic.modular.game.statistical.domain.PlayerStatisticalPool2;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * 玩家统计池(PlayerStatisticalPool2)表服务实现类
 *
 * @author cxy
 * @since 2025-01-15 10:49:52
 */
@Slf4j
@Service
public class PlayerStatisticalPool2ServiceImpl extends ServiceImpl<PlayerStatisticalPool2Mapper, PlayerStatisticalPool2>
        implements PlayerStatisticalPool2Service {
    @Resource
    private PlayerHeroMapper playerHeroMapper;
    @Resource
    private PlayerSkillMapper playerSkillMapper;
    @Resource
    private CoreExperienceServiceImpl coreExperienceService;
    @Resource
    private CoreHeroStarServiceImpl coreHeroStarService;
    @Resource
    private CoreSkillStarServiceImpl coreSkillStarService;
    @Resource
    private CoreHeroServiceImpl coreHeroService;

    public PlayerStatisticalPool2 select(Long playerId, int type) {
        LambdaQueryWrapper<PlayerStatisticalPool2> wrapper = new LambdaQueryWrapper<PlayerStatisticalPool2>()
                .eq(PlayerStatisticalPool2::getPlayerId, playerId)
                .eq(PlayerStatisticalPool2::getType, type);
        PlayerStatisticalPool2 one = getOne(wrapper);
        if (one == null) {
            PlayerStatisticalPool2 playerStatisticalPool2 = new PlayerStatisticalPool2();
            playerStatisticalPool2.setPlayerId(playerId);
            playerStatisticalPool2.setType(type);
            playerStatisticalPool2.setValue(0L);
            save(playerStatisticalPool2);
        }
        return one;
    }

    /**
     * 统计战斗信息
     */
    public void refreshStatistical(PvpPlayerInfo pvpPlayerInfo) {
        Long playerId = pvpPlayerInfo.getPlayerId();

        // 1	击败玩家 	击败玩家的最后一个城池
        int size = pvpPlayerInfo.getKilledPlayers().size();
        select(playerId, 1).add(size).updateById();

        // 2	击杀敌军	击杀敌方的兵力数量
        Long killForces = pvpPlayerInfo.getKillForces();
        select(playerId, 2).add(killForces).updateById();


        // 3	攻占城池	占领的城池数
        Integer maxCity = pvpPlayerInfo.getMaxCity() - 1;
        select(playerId, 3).add(maxCity).updateById();


        // 4	完成统一	统一
        Integer isRuler = pvpPlayerInfo.getIsRuler();
        select(playerId, 4).add(isRuler).updateById();


        // 5	完成对局	完成对局
        Integer isExit = pvpPlayerInfo.getIsExit();
        select(playerId, 5).add(isExit).updateById();

        // 6	累计游戏时间
        Long survivalTime = pvpPlayerInfo.obtainSurvivalTime();
        select(playerId, 6).add(survivalTime).updateById();
    }


    /**
     * 刷新拥有最高品质英雄数量
     */
    public void flush22(Long playerId) {
        Integer heroMaxQualityLv = coreHeroService.getHeroMaxQualityLv();
        long l22 = playerHeroMapper.countMaxQualityHero(playerId, heroMaxQualityLv);
        select(playerId, 22).add(l22).updateById();
    }

    /**
     * 刷新 满级 英雄数量
     */
    public void flush20(Long playerId) {
        Integer heroMaxLevel = coreExperienceService.getMaxLevel(1);
        long l20 = playerHeroMapper.countMaxLevelHero(playerId, heroMaxLevel);
        select(playerId, 20).add(l20).updateById();
    }

    /**
     * 刷新 满星 英雄数量
     */
    private void flush21(Long playerId) {
        Integer heroMaxStar = coreHeroStarService.getMaxStar();
        long l21 = playerHeroMapper.countMaxStarHero(playerId, heroMaxStar);
        PlayerStatisticalPool2 select = select(playerId, 21);
        select.setId(l21);
        updateById(select);
    }

    public void flushSkillInfo(Long playerId) {
        long l30 = playerSkillMapper.count(playerId);
        select(playerId, 30).add(l30).updateById();


        Integer skillMaxStar = coreSkillStarService.getMaxStar();
        long l31 = playerSkillMapper.countMaxStar(playerId, skillMaxStar);
        select(playerId, 31).add(l31).updateById();
    }

    public void logRisingStar(Long playerId) {
        flush21(playerId);
        select(playerId, 23).add(1).updateById();
    }


}
